package com.yanqu.road.dao.impl.activity.springdraw;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.ToStringSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.doublespring.data.DoubleSpringZlUserData;
import com.yanqu.road.entity.activity.springdraw.data.SpringDrawUserData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class SpringDrawUserDataDaoImpl extends TempDataObjectDao<SpringDrawUserData> {

    public static String selectSql = "select * from t_u_spring_draw_user_data where activityId = ? and userId = ?";
    public SpringDrawUserData getUserData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(selectSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String insertSql = "INSERT INTO `t_u_spring_draw_user_data`(`activityId`, `userId`, `level`, `drawNum`, `board`, `boardCreateTime`, `passFlag`, `selfSelectRewardMap`, `score`, `createTime`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    @Override
    protected boolean addData(SpringDrawUserData data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getLevel()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDrawNum()));
        //转JSON
        String boardStr = JSONObject.toJSONString(data.getBoard());
        para.put(i++, new DbParameter(Types.VARCHAR, boardStr));
        para.put(i++, new DbParameter(Types.BIGINT, data.getBoardCreateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPassFlag()));
        String json = "";
        if (!data.getSelfSelectRewardMap().isEmpty()) {
            json = StringUtils.mapToJson(data.getSelfSelectRewardMap());
        }
        para.put(i++, new DbParameter(Types.VARCHAR, json));
        para.put(i++, new DbParameter(Types.BIGINT, data.getScore()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getCreateTime()));
        if (getDb().executeNoneQuery(insertSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateSql = "UPDATE `t_u_spring_draw_user_data` SET `level` = ?, `drawNum` = ?, `board` = ?, `boardCreateTime` = ?, `passFlag` = ?, `selfSelectRewardMap` = ?, `score` = ? WHERE `activityId` = ? AND `userId` = ?";
    @Override
    protected boolean updateData(SpringDrawUserData data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getLevel()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDrawNum()));
        //转JSON
        String boardStr = JSONObject.toJSONString(data.getBoard());
        para.put(i++, new DbParameter(Types.VARCHAR, boardStr));
        para.put(i++, new DbParameter(Types.BIGINT, data.getBoardCreateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPassFlag()));
        String json = "";
        if (!data.getSelfSelectRewardMap().isEmpty()) {
            json = StringUtils.mapToJson(data.getSelfSelectRewardMap());
        }
        para.put(i++, new DbParameter(Types.VARCHAR, json));
        para.put(i++, new DbParameter(Types.BIGINT, data.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected SpringDrawUserData parseData(ResultSet rs) throws SQLException {
        SpringDrawUserData data = new SpringDrawUserData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setLevel(rs.getInt("level"));
        data.setDrawNum(rs.getInt("drawNum"));
        //转二维数组
        String boardStr = rs.getString("board");
        int[][] board = JSONObject.parseObject(boardStr, int[][].class);
        data.setBoard(board);
        data.setBoardCreateTime(rs.getLong("boardCreateTime"));
        data.setPassFlag(rs.getInt("passFlag"));
        if (!StringUtils.isNullOrEmpty(rs.getString("selfSelectRewardMap"))) {
            Map<Integer, String> map = StringUtils.jsonToMap2(rs.getString("selfSelectRewardMap"));
            for (Map.Entry<Integer, String> entry : map.entrySet()) {
                data.putSelfSelectRewardMap(entry.getKey(), entry.getValue());
            }
        }
        data.setScore(rs.getLong("score"));
        data.setCreateTime(rs.getLong("createTime"));
        return data;
    }
}
